/**
 * 客户资源录入脚本支持
 */
var key = null;
var modifydata = null;

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 初始化
	button_bind();
	init_editors();
} );

//--------------------------------------------------------------------------
//
//	consultant_client.php
//
//--------------------------------------------------------------------------

var _editor = [];

function button_bind()
{
	// 提交保存
	$( "#btnSubmit" ).bind( "click", function()
	{
		submit_form();
	} );

	// 选定咨询师
	$( "#btnSelect" ).click( select_consultant );
	$( "#btnSelect2" ).click( select_consultant );
}

function select_consultant(e)
{
	var nickname = $( this ).attr( "rel" );
	$("#user").find("option:contains('" + nickname + "')").prop("selected",true);
}

function init_editors()
{
	_editor = [];

	$( "div[contenteditable='true']" ).each( function()
	{
		_editor.push( $( this ).attr( "id" ) );
	} );
}

//--------------------------------------------------------------------------
//
//	数据提交
//
//--------------------------------------------------------------------------

function submit_form()
{
	var data = {};

	// 待更新的数据
	data.voteId = $( "#voteId" ).val();
	data.clientkey = $("#clientkey").val();
	// 组织提交数据
	data.user = $( "#user" ).val();
	data.desc = $( "#desc" ).html();

	submitter.post( "ask_allot_go.json", data, function( d )
	{
		alert("分配成功");
		window.self.location=document.referrer;

	}, 'json' );
}

//--------------------------------------------------------------------------
//
//	methods
//
//--------------------------------------------------------------------------

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submit_form();
	}
	else if( e.ctrlKey && e.shiftKey && e.which == 70 )// ctrl+shift+f
	{
		var edt = $( document.activeElement );
		var id = edt.attr( "id" );

		if( _editor.indexOf( id ) > -1 )
		{
			edt.html( htmlfilter( edt.html() ) );
		}
	}
}
